package com.offcn.sellergood.controller;

import com.offcn.entity.PageResult;
import com.offcn.entity.Result;
import com.offcn.sellergood.pojo.Brand;
import com.offcn.entity.StatusCode;
import com.offcn.sellergood.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Author: zola
 * @Date: 2021/9/7 - 09 - 07 - 20:05
 * @Description: com.offcn.sellergood.controller
 * @Version: 1.0
 */
@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;
    /**
     * @param: Long id
     * @return: Result(flag,code,msg,data)
     * @description: 主键查询
     * @description:  @PathVariable(“变量名”):是从URL中通过指定变量名找到值，然后把值注入给形参
     */
    @GetMapping("/find/{id}")
    public Result<Brand> findById(@PathVariable("id") Long id){
        try {
            Brand brand = brandService.findById(id);
            return new Result<>(true, StatusCode.OK,"主键查询成功",brand);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>(false,StatusCode.ERROR,"主键查询失败",null);
        }
    }

    // 查询所有
    @GetMapping("/find")
    public Result<List<Brand>> findAll(HttpServletRequest request){
        String token = request.getHeader("Authorization");
        System.out.println("请求头中的令牌是："+token);
        try {
            List<Brand> list = brandService.findAll();
            return new Result<>(true,StatusCode.OK,"查询所有成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>(false,StatusCode.ERROR,"查询所有失败",null);
        }
    }

    // 添加
    @PostMapping("/insert")
    public Result insert(@RequestBody Brand brand){
        try {
            brandService.insert(brand);
            return new Result(true,StatusCode.OK,"添加品牌成功",null);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"添加品牌失败",null);
        }
    }

    // 修改
    @PutMapping("/update/{id}")
    public Result update(@PathVariable("id") Long id,@RequestBody Brand brand){
        try {
            brand.setId(id);
            brandService.update(brand);
            return new Result(true,StatusCode.OK,"修改品牌成功",null);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"修改品牌失败",null);
        }
    }

    // 删除
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Long id){
        try {
            brandService.removeById(id);
            return new Result(true,StatusCode.OK,"删除品牌成功",null);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"删除品牌失败",null);
        }
    }

    // 模糊查询
    @PostMapping("/search")
    public Result<List<Brand>> findList(@RequestBody(required = false) Brand brand){
        try {
            List<Brand> list = brandService.findList(brand);
            return new Result<>(true,StatusCode.OK,"查询所有成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>(false,StatusCode.ERROR,"查询所有失败",null);
        }
    }

    // 分页查询
    @GetMapping("/search/{pageNumber}/{pageSize}")
    public Result<PageResult<Brand>> findPage(@RequestBody(required = false) Brand brand,
                                              @PathVariable int pageNumber, @PathVariable int pageSize){
        try {
            PageResult<Brand> page = brandService.findPage(brand,pageNumber, pageSize);
            return new Result<>(true,StatusCode.OK,"分页查询成功",page);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>(true,StatusCode.ERROR,"分页查询失败",null);
        }
    }

    // 查询品牌下拉列表
    @GetMapping("/selectOptions")
    public Result<List<Map>> selectOptions(){
        try {
            List<Map> list = brandService.selectOptions();
            return new Result<>(true,StatusCode.OK,"查询品牌下拉列表成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>(false,StatusCode.ERROR,"查询品牌下拉列表失败",null);
        }
    }
}
